//import and format psid data
clear all
do "$data/PSID/employment/J331273"
do "$data/PSID/employment/J331273_formats"

gen uniqid = ER30001*1000 + ER30002 //generation of uniqid identifier variable
duplicates report uniqid //no duplicates!
drop ER30002 ER30000 

//drop variables we don't care about
//loop over variables and drop what we can
ds uniqid, not
foreach var in `r(varlist)'{
	
	//fetch label
	local lab: variable label `var'
	
	//kill sequence/release/interview numbers
	if strpos("`lab'", "SEQUENCE") | strpos("`lab'", "RELEASE") | strpos("`lab'", "INTERVIEW") {
		drop `var'
		continue
	}
	
	//get year for individual files
	local num = word("`lab'", -1)
	
	//skip variables with labels that i don't like
	if length("`num'")!=2 | "`var'" == "V4373" {
		continue
	}	
	
	if `num'<20{ //2000s variable
		local year = 2000 + `num'
	}
	if `num'>20{ //1900s variable
		local year = 1900 + `num'
	}
	
	****rename according to label contents
	//age
	if strpos("`lab'", "AGE OF"){
		ren `var' age_`year'
	}
	
	//relation to head
	if strpos("`lab'", "RELATION"){
		ren `var' relate_`year'
	}
	
	//relation to head
	if strpos("`lab'", "EMPL"){
		ren `var' employment_`year'
	}
	
}

drop *1921


//fill in even years for age, relate
reshape long employment_ relate_, i(uniqid) j(year)
keep if relate == 1 | relate == 10 //household heads only

merge 1:1 uniqid year using "$data/PSID/income/psid_income", keep(match) nogen


save "$temp/psid_employment", replace

//end of dofile